package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.MoveStockScanTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2018/12/29
 * Description:移库倒库扫描表操作类
 */
public class MoveStockScanTableDao extends BaseDao<MoveStockScanTable, Long> {

    public MoveStockScanTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), MoveStockScanTable.class);
    }

    /**
     * 查找需要上传的数据
     *
     * @param mTaskNo  任务号
     * @param operType 1-装车，2-卸车，11-取消装车
     * @return list
     */
    public List<MoveStockScanTable> findNeedUploadData(String mTaskNo, int operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            where.and().eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);

            List<MoveStockScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取未上传的个数
     *
     * @param mTaskNo  任务号
     * @param operType 1-装车，2-卸车，11-取消装车
     * @return long
     */
    public long getUnUploadCount(String mTaskNo, List<String> operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            where.and().eq("taskNo", mTaskNo);
            where.and().in("operType", operType);
            return queryBuilder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 查找需要上传的子单
     *
     * @param mTaskNo        任务号
     * @param childWaybillNo 子单号
     * @return list
     */
    public List<MoveStockScanTable> findNeedUploadByChildWaybillNo(String mTaskNo, String childWaybillNo) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("childWaybillNo", childWaybillNo);

            List<MoveStockScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询该子单是否存在
     *
     * @param mTaskNo        任务号
     * @param childWaybillNo 子单号
     * @param operType       1-装车，2-卸车，11-取消装车
     * @return boolean
     */
    public boolean exitChildWaybillNo(String mTaskNo, String childWaybillNo, int operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            where.and().eq("childWaybillNo", childWaybillNo);
            return queryBuilder.queryForFirst() != null ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查找移库卸车强扫数据
     *
     * @param mTaskNo  任务号
     * @param userCode 用户编码
     * @param operType 1-装车，2-卸车，11-取消装车
     * @return list
     */
    public List<MoveStockScanTable> findUnloadForceScanData(String mTaskNo, String userCode, int operType, int isForceScan) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operCode", userCode);
            where.and().eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            where.and().eq("isForceScan", isForceScan);
            List<MoveStockScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据运单号查询子单
     *
     * @param mTaskNo   任务号
     * @param waybillNo 运单号
     * @param operType  1-装车，2-卸车
     * @return
     */
    public List<MoveStockScanTable> findDataByWaybillNo(String mTaskNo, String waybillNo, int operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            where.and().eq("waybillNo", waybillNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据子运单号查询子单
     *
     * @param mTaskNo
     * @param childWaybillNo
     * @param operType
     * @return
     */
    public List<MoveStockScanTable> findDataByChildWaybillNo(String mTaskNo, String childWaybillNo, int operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            where.and().eq("childWaybillNo", childWaybillNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询装卸车数据
     *
     * @param mTaskNo  任务号
     * @param operType 1-装车，2-卸车
     * @return
     */
    public List<MoveStockScanTable> findLoadData(String mTaskNo, int operType) {
        try {
            QueryBuilder<MoveStockScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteByTaskNo(String taskNo, int loadType) {
        List<MoveStockScanTable> list = findLoadData(taskNo, loadType);
        try {
            delete(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
